﻿//
// Author: rgy
// Date: 2016-03-18 09:23:00
//

using SLua;
using UnityEngine;

namespace SoarD.Tween
{
    public enum LoopMode
    {
        None,
        Loop,
        PingPong,
    }
    public class SoarDImplement
    {
        private uint m_id;
        public uint id { get { return m_id; } }
        public GameObject gameObject;
        private TweenObjectBase _tween;
        private bool m_pause;
        private LoopMode _loopMode = LoopMode.None;
        private OnCompleteDel _completeHandle;
        public delegate void OnCompleteDel();
        public bool IsDestroy { get; private set; }
        public SoarDImplement Init(uint uid, TweenObjectBase tween)
        {
            m_id = uid;
            _tween = tween;
            gameObject = tween.gameObject;
            _completeHandle = null;
            m_pause = false;
            IsDestroy = false;
            _loopMode = LoopMode.None;
            return this;
        }

        public void Excute(float dt)
        {
            if (_tween.IsDestroy)
            {
                IsDestroy = true;
                return;
            }
            if (m_pause)
                return;
            if (_tween.Excute(dt))
            {
                switch (_loopMode)
                {
                    case LoopMode.Loop:
                        _tween.LoopInit();
                        return;
                    case LoopMode.PingPong:
                        _tween.PingPongInit();
                        return;
                }
                IsDestroy = true;
            }
        }

        public SoarDImplement SetLoopMode(LoopMode mode)
        {
            _loopMode = mode;
            return this;
        }

        public SoarDImplement SetLoop(bool isLoop = true)
        {
            if (isLoop)
            {
                _loopMode = LoopMode.Loop;
            }
            return this;
        }

        public SoarDImplement SetPingPong(bool isPingPong = true)
        {
            if (isPingPong)
            {
                _loopMode = LoopMode.PingPong;
            }
            return this;
        }

        public SoarDImplement SetOnComplete(OnCompleteDel completeDel)
        {
            _completeHandle = completeDel;
            return this;
        }

        public void Pause()
        {
            m_pause = true;
        }

        public void Resume()
        {
            m_pause = false;
        }

        [DoNotToLua]
        public void SDCancel()
        {
            _completeHandle = null;
            IsDestroy = true;
            m_id = 0;
        }

        public void Destroy()
        {

            if (!_tween.IsDestroy && _completeHandle != null)
            {
                _completeHandle();
            }
            _tween.Destroy();
            SoarDTween.Instance.ImplementPool.Recycle(this);
        }
    }
}
